package com.tcat.item.web;

import com.tcat.item.pojo.SpecGroup;
import com.tcat.item.pojo.SpecParam;
import com.tcat.item.pojo.Specification;
import com.tcat.item.pojo.Stock;
import com.tcat.item.service.SpecificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "规格参数管理")
@RestController
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;




    /**
     * 根据分类id查询分类组    正确
     * @param cid
     * @return
     */
    @ApiOperation(value = "根据分类id查询分类组")
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupsByCid(@PathVariable("cid")Long cid){
        List<SpecGroup> groups=this.specificationService.querySpecGroupByCid(cid);
        if(CollectionUtils.isEmpty(groups)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(groups);
    }


    /**
     * 修改一个规格参数模板
     * @param
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateSpecification(Specification specification){
        this.specificationService.updateSpecification(specification);
        return ResponseEntity.status(HttpStatus.OK).build();
    }



    /**
     * 保存一个规格参数模板
     * @param specification
     * @return
     */
    @ApiOperation(value = "保存一个规格参数模板")
    @PostMapping
    public ResponseEntity<Void> saveSpecification(Specification specification){
        this.specificationService.saveSpecification(specification);
        return ResponseEntity.status(HttpStatus.OK).build();
    }



    /**
     * 删除模板
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleteSpecification(@PathVariable("id") Long id){
        Specification specification = new Specification();
        specification.setCategoryId(id);
        this.specificationService.deleteSpecification(specification);
        return ResponseEntity.status(HttpStatus.OK).build();
    }






    /**
     * 根据条件查询规格参数  正确
     * @param gid
     * @return
     */
    @ApiOperation(value = "根据条件查询规格参数")
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> queryParams(
            @RequestParam(value="gid", required = false) Long gid,
            @RequestParam(value="cid", required = false) Long cid,
            @RequestParam(value="searching", required = false) Boolean searching
    ){
        List<SpecParam> params=this.specificationService.queryParams(gid,cid,searching);
        if(CollectionUtils.isEmpty(params)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(params);
    }




    /**
    * 查询商品分类对应的规格参数模板
    * @param id
    * @return
    */
    @GetMapping("{id}")
    public ResponseEntity<String> querySpecificationByCategoryId(@PathVariable("id") Long id){
        Specification spec = this.specificationService.queryById(id);
        if (spec == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
       }
       return ResponseEntity.ok(spec.getSpecifications());
   }










//-----------------------------------------------


    /**
     * 增加商品规格组   正确
     * @param specGroup
     * @return
     */

     @PostMapping("group")
     public ResponseEntity<Void> saveSpecGroup(@RequestBody SpecGroup specGroup) {
     specificationService.saveSpecGroup(specGroup);
     return ResponseEntity.status(HttpStatus.CREATED).build();
     }

    /**
     * 删除商品规格组   正确
     * @param id
     * @return
     */
    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> deleteSpecGroup(@PathVariable("id") Long id) {
        specificationService.deleteSpecGroup(id);
        return ResponseEntity.ok().build();

    }

    /**
     * 更新商品规格组    正确
     * @param specGroup
     * @return
     */
    @PutMapping("group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroup specGroup) {
        specificationService.updateSpecGroup(specGroup);
        return ResponseEntity.ok().build();
    }

    /**
     * 增加商品规格参数  正确
     * @param specParam
     * @return
     */
    @PostMapping("param")
    public ResponseEntity<Void> saveSpecParam(@RequestBody SpecParam specParam) {
        specificationService.saveSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除商品规格参数  正确
     * @param id
     * @return
     */
    @DeleteMapping("param/{id}")
    public ResponseEntity<Void> deleteSpecParam(@PathVariable("id") Long id) {
        specificationService.deleteSpecParam(id);
        return ResponseEntity.ok().build();

    }

    /**
     * 更新商品规格参数    正确
     * @param specParam
     * @return
     */
    @PutMapping("param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParam specParam) {
        specificationService.updateSpecParam(specParam);
        return ResponseEntity.ok().build();
    }
    /**
     * 根据分类查询规格组及组内参数
     * @param cid
     * @return
     */
    @ApiOperation(value = "根据分类查询规格组及组内参数")
    @GetMapping("group")
    public ResponseEntity<List<SpecGroup>> queryListByCid(@RequestParam("cid")Long cid){
        List<SpecGroup> list=specificationService.queryListByCid(cid);
        if(CollectionUtils.isEmpty(list)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }



    @GetMapping("/countSkus")
    public int querySkus(){
        return specificationService.countSkus();
    }

    @GetMapping("/queryStock/{id}")
    public Stock queryStock(@RequestParam("id") Long id){
        return specificationService.queryStock(id);
    }

}
